// 滚动动画效果
document.addEventListener('DOMContentLoaded', function() {
  // 滚动动画
  const animatedElements = document.querySelectorAll('.animate');
  
  function checkAnimation() {
    animatedElements.forEach(element => {
      const elementTop = element.getBoundingClientRect().top;
      const windowHeight = window.innerHeight;
      
      // 当元素进入视口时添加动画类
      if (elementTop < windowHeight - 50) {
        // 添加延迟效果
        const delay = element.getAttribute('data-delay') || 0;
        setTimeout(() => {
          element.classList.add('animated');
        }, delay);
      }
    });
  }
  
  // 初始检查
  checkAnimation();
  
  // 滚动时检查
  window.addEventListener('scroll', checkAnimation);
  
  // 服务卡片轮播
  const servicesCarousel = document.querySelector('.services-carousel');
  if (servicesCarousel) {
    const cardsContainer = servicesCarousel.querySelector('.services-cards');
    const cards = servicesCarousel.querySelectorAll('.service-card');
    const prevBtn = document.getElementById('services-prev');
    const nextBtn = document.getElementById('services-next');
    
    let currentIndex = 0;
    const cardWidth = cards[0].offsetWidth + 20; // 卡片宽度 + 间距
    const visibleCards = window.innerWidth > 991 ? 3 : window.innerWidth > 576 ? 2 : 1;
    const maxIndex = cards.length - visibleCards;
    
    // 自动轮播
    let autoScrollInterval = setInterval(nextSlide, 5000);
    
    function nextSlide() {
      if (currentIndex < maxIndex) {
        currentIndex++;
      } else {
        currentIndex = 0;
      }
      updateCarousel();
    }
    
    function prevSlide() {
      if (currentIndex > 0) {
        currentIndex--;
      } else {
        currentIndex = maxIndex;
      }
      updateCarousel();
    }
    
    function updateCarousel() {
      cardsContainer.style.transform = `translateX(-${currentIndex * cardWidth}px)`;
    }
    
    // 按钮控制
    if (prevBtn && nextBtn) {
      prevBtn.addEventListener('click', function() {
        clearInterval(autoScrollInterval);
        prevSlide();
        autoScrollInterval = setInterval(nextSlide, 5000);
      });
      
      nextBtn.addEventListener('click', function() {
        clearInterval(autoScrollInterval);
        nextSlide();
        autoScrollInterval = setInterval(nextSlide, 5000);
      });
    }
    
    // 响应窗口大小变化
    window.addEventListener('resize', function() {
      const newVisibleCards = window.innerWidth > 991 ? 3 : window.innerWidth > 576 ? 2 : 1;
      const newMaxIndex = cards.length - newVisibleCards;
      
      if (currentIndex > newMaxIndex) {
        currentIndex = newMaxIndex;
      }
      
      updateCarousel();
    });
  }
  
  // 数字计数器动画
  const counters = document.querySelectorAll('.counter');
  
  function startCounting(counter) {
    const target = parseInt(counter.getAttribute('data-target'));
    let count = 0;
    const increment = target / 50; // 50步完成计数
    
    const updateCounter = () => {
      if (count < target) {
        count += increment;
        counter.textContent = Math.ceil(count);
        setTimeout(updateCounter, 30);
      } else {
        counter.textContent = target;
      }
    };
    
    updateCounter();
  }
  
  // 使用Intersection Observer监测计数器元素
  const counterObserver = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        startCounting(entry.target);
        counterObserver.unobserve(entry.target);
      }
    });
  }, { threshold: 0.5 });
  
  counters.forEach(counter => {
    counterObserver.observe(counter);
  });
  
  // 自动切换内容
  const autoScrollContainers = document.querySelectorAll('.auto-scroll-container');
  
  autoScrollContainers.forEach(container => {
    const wrapper = container.querySelector('.auto-scroll-wrapper');
    const items = container.querySelectorAll('.auto-scroll-item');
    const itemCount = items.length;
    let currentItem = 0;
    
    function scrollToNext() {
      currentItem = (currentItem + 1) % itemCount;
      wrapper.style.transform = `translateY(-${currentItem * 100}%)`;
    }
    
    // 每4秒切换一次
    setInterval(scrollToNext, 4000);
  });
});